package com.mingqijia.gassafety.job.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.db.entity.Dictionary;
import com.mingqijia.gassafety.db.mapper.DictionaryMapper;
import com.mingqijia.gassafety.job.service.DictionaryService;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 字典项 服务实现类
 * </p>
 *
 * @author zhangfei
 * @since 2022-02-23
 */
@Service
@Slf4j
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Autowired
    DictionaryMapper dictionaryMapper;

    @Override
    public Dictionary selectValueByAlias(String mbpOrgCode) {
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.or().eq(Dictionary::getDictCode,"mbp_org_mapping")
        .eq(Dictionary::getIsDeleted,0).eq(Dictionary::getAlias,mbpOrgCode);
        List<Dictionary> dictionaries = dictionaryMapper.selectList(queryWrapper);
        log.info("selectValueByAlias-dictionaries:{}", JSONObject.toJSONString(dictionaries));
        if (CollectionUtil.isEmpty(dictionaries)) throw new BizException("映射不到相关组织信息:"+mbpOrgCode);
        return dictionaries.get(0);
    }

    @Override
    public String getChannel(String spId) {
        LambdaQueryWrapper<Dictionary> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dictionary::getDictCode, "Call_channel_configuration").eq(Dictionary::getIsDeleted, 0)
                .eq(Dictionary::getValue, spId);
        List<Dictionary> dictionaries = dictionaryMapper.selectList(lqw);
        log.info("getChannel-dictionaries:{}", JSONObject.toJSONString(dictionaries));
        if (CollectionUtil.isEmpty(dictionaries)) {
            return null;
        }else{
            return dictionaries.get(0).getDescription();

        }
    }

    @Override
    public List<Dictionary> selectDataOrgIdBySpId(String spId) {
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.or().eq(Dictionary::getDictCode,"mbp_org_mapping")
                .eq(Dictionary::getIsDeleted,0).eq(Dictionary::getValue,spId);
        List<Dictionary> dictionaries = dictionaryMapper.selectList(queryWrapper);
        log.info("selectValueByAlias-dictionaries:{}", JSONObject.toJSONString(dictionaries));
        if (CollectionUtil.isEmpty(dictionaries)) throw new BizException("映射不到相关sp信息:"+spId);
        return dictionaries;
    }
}
